#include "Frame/calibration.hpp"
#include <sstream>

calibration::calibration(Process *_process):Frame(_process)
{
    TempFlag = 0;
}

calibration::~calibration()
{
     delete io;
}

int calibration::Work()
{
    for (;;)
    {
        this->DoEvents(10);
        if(!state->RunningFlag)
            break;

        if (state->PausingFlag)
        {
            if(!state->PreFrameFlag)
            {
                process->Do(this->model->src_img);
                Notify(this->model->Result());
                process->AddFrameFlag = false;
            }
            else
            {
                if(!preFrame())
                    break;
                Log(this->model->GetInfo());
                state->PreFrameFlag = !state->PreFrameFlag;
                //Log("Pre OK");
            }
        }
        else
        {
            if(!nextFrame())
                break;
            state->Pause();
            process->m_count++;
            Log(this->model->GetInfo());
        }
    }
    this->model->OutputBoxes();
    return -1;
}

int calibration::preFrame()
{
    if(process->m_count==1)
    {
        Log("This is the first frame!");
        process->m_count = 2;
    }
    process->m_count--;
    string id_c;
    std::stringstream ss;
    ss << process->m_count;
    ss >> id_c;
    //process->Swap(this->model->src_img);
    if(this->io->GetImageFromImage(this->model->src_img, "Temp/"+id_c + ".jpg"))
    {
        this->model->PreFrame();
        for(size_t i=0;i<state->selection.size();i++)
        {
            state->selection[i] = this->model->boxes[i];
            state->TrackObject[i] = -1;
        }
        return 1;
    }
    return 0;
}

int calibration::nextFrame()
{
    if(process->m_count == TempFlag)
    {
        process->Swap(this->model->src_img);
        if(this->io->GetImage(this->model->src_img))
        {
            string id_c;
            std::stringstream ss;
            ss << process->m_count+1;
            ss >> id_c;
            this->io->WriteImage(this->model->src_img, ("Temp/"+id_c + ".jpg"));
            TempFlag = process->m_count + 1;
            this->model->AddFrame();

            /*
            for(size_t i = 0;i<this->model->boxes.size();i++)
            {
            	Image box_image(this->model->src_img, this->model->boxes[i]);
            	string id_i;std::stringstream st;st << i;st >> id_i;
            	if(box_image.width > 50 && box_image.height > 50)
            		this->io->WriteImage(box_image, ("Output/"+id_i + "_" + id_c + ".jpg"));
            }
            */
            return 1;
        }
    }
    else
    {
        string id_c;
        std::stringstream ss;
        ss << process->m_count;
        ss >> id_c;
        process->Swap(this->model->src_img);
        if(this->io->GetImageFromImage(this->model->src_img, "Temp/"+id_c + ".jpg"))
        {
            this->model->AddFrame();
            return 1;
        }
    }
    return 0;
}
